package cn.cmcc.intertelecom.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.cmcc.intertelecom.bean.Msg;
import cn.cmcc.intertelecom.bean.PowerGroup;
import cn.cmcc.intertelecom.bean.Role;
import cn.cmcc.intertelecom.service.PowerGroupService;
import cn.cmcc.intertelecom.service.RoleGroupService;
import cn.cmcc.intertelecom.service.RoleService;
import cn.cmcc.intertelecom.utils.ArchivesLog;

@Controller
public class RoleController {
	@Autowired
	private RoleService roleService;
	@Autowired
	private PowerGroupService powerGroupService;
	@Autowired
	private RoleGroupService roleGroupService;
	
	
	/**
	 * 删除 角色
	 * @param department
	 * @param result
	 * @return
	 */
	@ArchivesLog(operationPower="delete_role")
	@ResponseBody
	@RequestMapping(value="/role/{roleIds}" ,method=RequestMethod.DELETE)
	public Msg deleteRole(@PathVariable(value="roleIds")String roleIds){
		if(roleIds.contains("-")){
			String[] str = roleIds.split("-");
			List<Integer> ids=new ArrayList<>();
			for (String string : str) {
				ids.add(Integer.parseInt(string));
			}
			////删除用户-角色 映射表里面的所有关于该角色的数据
			roleGroupService.deleteBatchByRoleIds(ids);
			
			//删除权限-角色 映射表里面的所有关于该角色的数据
			powerGroupService.deleteBatchByRoleIds(ids);
			//删除角色表中的数据
			roleService.deleteBatch(ids);
			
		}else{
			//删除用户-角色 映射表里面的所有关于该角色的数据
			roleGroupService.deleteByRoleId(Integer.parseInt(roleIds));
			//删除权限-角色 映射表里面的所有关于该角色的数据
			powerGroupService.deleteByRoleId(Integer.parseInt(roleIds));
			//删除角色表中的数据
			roleService.deleteDept(Integer.parseInt(roleIds));
			
		}
		return Msg.success();
	}
	

	/**
	 * 配置角色的权限 
	 * @param roleGroup
	 * @return
	 */
	@ArchivesLog(operationPower="update_role")
	@ResponseBody
	@RequestMapping(value="role" ,method=RequestMethod.PUT)
	public Msg configPower(PowerGroup powerGroup){
		System.out.println(powerGroup);
		powerGroupService.configPower(powerGroup);
		return Msg.success();
	}
	/**
	 * 储存角色
	 * @param role
	 * @return
	 */
	
	@ArchivesLog(operationPower="new_role")
	@ResponseBody
	@RequestMapping(value="role" ,method=RequestMethod.POST)
	public Msg saveRole(Role role){
		roleService.insertRole(role);
		return Msg.success();
	}
	
	/**
	 * 根据角色id获取角色 已经该角色的权限
	 * @param roleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/role/{roleId}" ,method=RequestMethod.GET)
	public Msg getAllRoleById(@PathVariable(value="roleId")Integer roleId){
		Role role=roleService.getRoleWithPowersById(roleId);
		return Msg.success().add("role", role);
	}
	
	/**
	 * 从数据库获取所以的角色 分页的方式显示
	 * @param pn
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="roles" ,method=RequestMethod.GET)
	public Msg getAllRoleWithPage(@RequestParam(value="pn",defaultValue="1")Integer pn){
		System.out.println(pn);
		
		PageHelper.startPage(pn, 5);
		List<Role> list=roleService.getAllWithModule();
		PageInfo<Role> pageInfo=new PageInfo<>(list);
		return Msg.success().add("pageInfo", pageInfo);
	}
	
	/**
	 * 仅仅获取所有的角色而已
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/roless" ,method=RequestMethod.GET)
	public Msg getAllRole(){
		
		List<Role> list=roleService.getAllRole();
		
		return Msg.success().add("list", list);
	}
	
	
	
}
